const Sequelize = require('sequelize');
const configs = require('./db_config');
var sequelize = new Sequelize(configs.db.basename,configs.db.user, configs.db.pass, {
  host: configs.db.host,
  dialect: 'mysql',
  pool: {
    max: 5,
    min: 0,
    idle: 30000
  },
  define: {
    timestamps: false
  }
});
// console.log(dbcofig);
// 连接数据库
sequelize
  .authenticate()
  .then(() => {
    console.log('数据库连接成功.');
  })
  .catch(err => {
    console.error('数据库连接失败:', err);
  });

// 用户表
const user = sequelize.define('cs_user',{
  id:{
    type: Sequelize.INTEGER(10),
    primaryKey: true,
    autoIncrement:true
  },
  name:Sequelize.STRING,
  nick:Sequelize.STRING,
  password:Sequelize.STRING,
  email:Sequelize.STRING,
  status:Sequelize.INTEGER,
  gender:Sequelize.INTEGER,
  city:Sequelize.STRING,
  is_vip:Sequelize.INTEGER,
  head_pic:Sequelize.STRING,
  vip_end:Sequelize.DATE,
  money:Sequelize.INTEGER,
  update_time:Sequelize.DATE,
},{timestamps: false,freezeTableName:true});

// 小说基础表
const novel_base = sequelize.define('cs_novel_base',{
  id:{
    type: Sequelize.INTEGER(11),
    primaryKey: true,
    autoIncrement:true
  },
  author:Sequelize.STRING,
  collect_id:Sequelize.INTEGER(11),
  name:Sequelize.STRING,
  collect_count:Sequelize.INTEGER(8),
  description:Sequelize.STRING,
  logo:Sequelize.STRING,
  banner:Sequelize.STRING,
  is_end:Sequelize.INTEGER(1),
  keywords:Sequelize.STRING,
  type_id:Sequelize.STRING,
  title_count:Sequelize.INTEGER(4),
  view_count:Sequelize.INTEGER(11),
  status:Sequelize.INTEGER(1),
  update_time:Sequelize.DATE,
},{timestamps: false,freezeTableName:true})

// 小说章节表
const novel_chapter = sequelize.define('cs_novel_chapter',{
  id:{
    type: Sequelize.INTEGER(10),
    primaryKey: true,
    autoIncrement:true
  },
  n_id:Sequelize.INTEGER(11),
  collect_id:Sequelize.INTEGER(11),
  title:Sequelize.STRING,
  pay_money:Sequelize.INTEGER(4),
  status:Sequelize.INTEGER(1),
  section_num:Sequelize.INTEGER(8),
  update_time:Sequelize.DATE,
},{timestamps: false,freezeTableName:true})

// 小说详情表
const novel_detail = sequelize.define('cs_novel_detail',{
  id:{
    type: Sequelize.INTEGER(10),
    primaryKey: true,
    autoIncrement:true
  },
  t_id:Sequelize.INTEGER(11),
  title:Sequelize.STRING,
  status:Sequelize.INTEGER(1),
  content:Sequelize.TEXT,
  update_time:Sequelize.DATE,
},{timestamps: false,freezeTableName:true})

module.exports = {
  db:sequelize,
  user:user,
  novel_base:novel_base,
  novel_chapter:novel_chapter,
  novel_detail:novel_detail
};
